package it.koutifaris.travianmap.view.serversettingsview;

import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.TravianMap;
import it.koutifaris.travianmap.command.CommandOnClickListener;
import it.koutifaris.travianmap.context.ContextRetreiver;
import it.koutifaris.travianmap.context.HasApplicationContex;
import it.koutifaris.travianmap.dal.TravianServerService;
import it.koutifaris.travianmap.dal.TravianServerSettingsService;
import it.koutifaris.travianmap.dal.TravianServerSettingsService.TravianServerSettings;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ServerViewController extends HasApplicationContex implements Observer {

	private final ServerViewModel model;
	private final ServerView view;

	public ServerViewController() {
		model = new ServerViewModel("ts1.travian.it", false, false);
		model.addObserver(this);
		view = new ServerView();
	}

	void show() {
		getActivity().setContentView(R.layout.server);
		configure();
		populate(model);
	}

	public void show(TravianServerService.TravianServer server) {
		getActivity().setContentView(R.layout.server);
		configure();

		TravianServerSettingsService settingsService = getTravianMapContext().getServerServiceFactory().travianServerSettingsService();
		TravianServerSettings settings = settingsService.getSettginsFor(server.getServerId());
		populate(settings);
		settingsService.close();
	}

	@Override
	public void update(Observable o, Object arg) {
		view.getServerTextBox().setText(model.getServer());
		view.getAutoUpdate().setChecked(model.isAutoUpdate());
		view.getWifiOnly().setChecked(model.isWifiOnly());
	}

	private void populate(TravianServerSettings settings) {
		model.setServer(settings.getServer());
		model.setAutoUpdate(settings.isAutoUpdate());
		model.setWifiOnly(settings.isWifiOnly());

		model.notifyObservers();
	}

	private void configure() {
		view.getOkButton().setOnClickListener(
				new CommandOnClickListener(
						new OKClickCommand(
								view,
								model,
								new MapUrlVerifier(new OnOkHandler(this))))
				);

		view.getCancelButton().setOnClickListener(new CommandOnClickListener(new CancelClickCommand()));

		view.getWifiOnly().setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				model.setWifiOnly(isChecked);
			}
		});

		view.getAutoUpdate().setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				model.setAutoUpdate(isChecked);
			}
		});

		view.getServerTextBox().addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				model.setServer(s.toString());
			}
		});
	}

	public static void finishAndReturnToRootActivity() {
		Activity activity = ContextRetreiver.contextRetreiver().getCurrentContext().getActivity();
		Intent intent = new Intent(activity, TravianMap.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		activity.startActivity(intent);
	}

	public ServerView getView() {
		return view;
	}

	public ServerViewModel getModel() {
		return model;
	}

}
